package com.beecloud.microservice.user.web.controller;

import com.beecloud.microservice.user.commom.RichString;
import com.beecloud.microservice.user.domain.dto.*;
import com.beecloud.microservice.user.domain.error.ParameterWrongException;
import com.beecloud.microservice.user.service.UserService;
import com.beecloud.microservice.user.web.parameter.NotificationParam;
import com.beecloud.microservice.user.web.parameter.ResetPasswordParam;
import com.beecloud.microservice.user.web.parameter.UserRegisterParam;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.beecloud.microservice.user.commom.RichString.nonBlank;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.*;

@Api(tags = "用户接口", description = "用户管理Ａpi")
@RestController
@RequestMapping(path = "/users", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
public class UserController implements UserApi {
    @Autowired
    private UserService userService;

    @RequestMapping(method = POST)
    @Override
    public ResponseEntity<UserDTO> userRegister(@RequestBody UserRegisterParam userParam) {
        boolean validate = userParam != null && nonBlank(userParam.getDriverLicense(), userParam.getPassword(), userParam.getUserName());
        if(!validate)
            throw new ParameterWrongException("DriverLicense is required;　password is required; userName is required.");
        UserDTO user = userService.createUser(userParam);
        return ResponseEntity.ok(user);
    }

    @RequestMapping(path = "/{id}", method = GET)
    @Override
    public  ResponseEntity<UserDTO> queryUserDetail(@PathVariable String id) {
        if(!RichString.nonBlank(id))
            throw new ParameterWrongException("Id is required.");
        UserDTO userDTO = userService.queryUserById(id);
        return ResponseEntity.ok(userDTO);

    }

    @RequestMapping(method = GET)
    @Override
    public ResponseEntity<UserPageDTO> queryUsers(
            @RequestParam String name,
            @RequestParam(defaultValue = "1", required = false) int page,
            @RequestParam(defaultValue = "20", required = false) int pageSize) {
        return ResponseEntity.ok(userService.queryUsers(name, page, pageSize));
    }

    @RequestMapping(method = GET, path = "/batch")
    @Override
    public ResponseEntity<List<UserDTO>> queryUsersByIds(@RequestParam String ids) {
        if(!RichString.nonBlank(ids))
            throw new ParameterWrongException("Ids is required.");
        List<UserDTO> userDTOs = userService.queryUsers(ids.split(","));

        return ResponseEntity.ok(userDTOs);
    }

    @RequestMapping(path = "/{id}/password", method = PUT)
    @Override
    public ResponseEntity<UserDTO> resetPassword(@PathVariable String id, @RequestBody ResetPasswordParam param) {
        boolean isValidate = param != null && nonBlank(id, param.getNewPassword(), param.getOldPassword());
        if(!isValidate)
            throw new ParameterWrongException("id is required; old password is required; new password is required.");
        UserDTO userDTO = userService.changePassword(id, param.getOldPassword(),
                param.getNewPassword());
        return ResponseEntity.ok(userDTO);
    }

    @RequestMapping(path = "/{id}/orders", method = GET)
    @Override
    public ResponseEntity<List<OrderDTO>> queryUserOrders(@PathVariable String id,
                                          @RequestParam String status,
                                          @RequestParam int page,
                                          @RequestParam int pageSize) {
        if(!RichString.nonBlank(id))
            throw new ParameterWrongException("id is required.");
        return ResponseEntity.ok(null);
    }

    @RequestMapping(path = "/{id}/notifications", method = GET)
    @Override
    public ResponseEntity<NotificationPageDTO> queryNotifications(
            @PathVariable String id,
            @RequestParam int status,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "30") int pageSize) {
        if(!RichString.nonBlank(id))
            throw new ParameterWrongException("id is required.");
        return ResponseEntity.ok(null);
    }

    @RequestMapping(path = "/{id}/notifications", method = POST)
    @Override
    public ResponseEntity<NotificationDTO> createNotification(@PathVariable String id,
                               @RequestBody NotificationParam notificationParam) {
        if(!RichString.nonBlank(id))
            throw new ParameterWrongException("id is required.");
        return ResponseEntity.ok(null);
    }
}
